package ntd.util.frame.javacv.remote;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.StaticLog;
import ntd.util.frame.javacv.action.bean.AIServerDo;
import ntd.util.frame.javacv.config.AiConfig;
import ntd.util.frame.javacv.tool.ToolFtpSync;
import ntd.util.util.ToolDuration;

import java.util.HashMap;

public class AiVisionRemote {

    public ToolFtpSync toolFtpSync;
    public AIServerDo aiServerDo;

    public AiVisionRemote(AIServerDo aiServerDo) {
        this.aiServerDo = aiServerDo;
    }

    public ToolFtpSync initFtpSync() {
        return new ToolFtpSync(aiServerDo.getUri(), aiServerDo.getFtpPort(), aiServerDo.getFtpUsername(), aiServerDo.getFtpPassword());
    }

    public ToolFtpSync getFtpSync() {
        if (ObjectUtil.isNull(this.toolFtpSync)) {
            this.toolFtpSync = this.initFtpSync();
        }
        return toolFtpSync;
    }

    public void analyse(String path, String aiModeType,String aiModePort) {
        ToolDuration.duration(() -> {
            HashMap<String, Object> params = MapUtil.newHashMap();
            // source -> D:\AI_result/Algae-20221012-e89132ae0919441d81dae17e8bca1ff3
            params.put("source", path);
            params.put("model", aiModeType);
            String res = HttpUtil.get(StrUtil.format("http://{}:{}/vision", aiServerDo.getUri(), aiModePort), params);
            // 删除原始文件
//            toolFtp.delFile(StrUtil.format("{}/{}", imgBasePath, imgName));
            StaticLog.info("分析执行结果-{}", res);
        }, StrUtil.format("调用远程[{}:{}]机器视觉[{}]分析[{}]", aiServerDo.getUri(), aiModePort, aiModeType, path));
    }

    public String analysisUrl(String model, String source) {
        return StrUtil.format("{}vision?model={}&source={}", aiServerDo.baseRealUrl(), model, source);
    }
}
